package com.warehouse_management.market.controller;

import com.alibaba.fastjson.JSON;
import com.warehouse_management.config.PageModel;
import com.warehouse_management.market.model.Custom;
import com.warehouse_management.market.model.CustomCondition;
import com.warehouse_management.market.model.Provin;
import com.warehouse_management.market.service.IMarketService;
import com.warehouse_management.system.model.Job;
import com.warehouse_management.login.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @PACKAGE_NAME: com.warehouse_management.market.controller
 * @NAME: MarketController
 * @USER: WanWenCheng
 * @DATE: 2022/10/2
 * @TIME: 18:49
 * @Description: Market控制层(营销)
 *
 */
@Controller
@RequestMapping("/market")
public class MarketController {

    @Autowired
    private IMarketService marketService;

    public IMarketService getMarketService() {
        return marketService;
    }

    public void setMarketService(IMarketService marketService) {
        this.marketService = marketService;
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.market.controller
     * @NAME: queryAllMarket
     * @USER: WanWenCheng
     * @DATE: 2022/10/2
     * @TIME: 23：35
     * @Description: 分页查询
     * */
    @RequestMapping("/queryAllMarket")
    @ResponseBody
    public String queryAllMarket(Custom custom, PageModel pageModel){
        PageModel<Custom> pageModel1 = marketService.PageJobPosition(custom, pageModel);
        if (pageModel1.getData()!=null){
            if (!pageModel1.getData().isEmpty()){
                for (int i=0;i<pageModel1.getData().size();i++){
                    if (pageModel1.getData().get(i).getProvin()==null){
                        pageModel1.getData().get(i).setProvin(new Provin());
                        pageModel1.getData().get(i).getProvin().setP_name("还未设置");
                    }
                }
                return JSON.toJSONString(pageModel1);
            }
        }
        return null;
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.market.controller
     * @NAME: queryAllMarket
     * @USER: WanWenCheng
     * @DATE: 2022/10/2
     * @TIME: 23：59
     * @Description: 修改状态
     * */
    @RequestMapping("/updateMarketCustomByState/{customid}/{status}")
    @ResponseBody
    public String updateMarketCustomByState(Custom custom, @PathVariable("customid") String customid, @PathVariable("status") String status){
        CustomCondition condition=new CustomCondition();
        condition.setCustomid(Integer.parseInt(customid));
        condition.setStatus(status);
        int i = marketService.updateMarketCustomByState(condition);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.market.controller
     * @NAME: customView
     * @USER: WanWenCheng
     * @DATE: 2022/10/3
     * @TIME: 23：35
     * @Description: 查询客户详情
     * */
    @RequestMapping("/queryOneCustom/{customid}")
    public String queryOneCustom(@PathVariable("customid") String customid, HttpSession session){
        CustomCondition condition=new CustomCondition();
        condition.setCustomid(Integer.parseInt(customid));
        Custom custom = marketService.queryOneCustom(condition);
        session.setAttribute("oneCustom",custom);
        return "market/customer/customerView";
    }

    @RequestMapping("/customView")
    @ResponseBody
    public String customView(HttpSession session){
        Custom oneCustom = (Custom) session.getAttribute("oneCustom");
        if (oneCustom!=null){
            if (oneCustom.getProvin()==null){
                oneCustom.setProvin(new Provin());
                oneCustom.getProvin().setP_name("还未设置");
            }
            return JSON.toJSONString(oneCustom);
        }else {
            return JSON.toJSONString("false");
        }
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.market.controller
     * @NAME: updateCustom
     * @USER: WanWenCheng
     * @DATE: 2022/10/3
     * @TIME: 23：35
     * @Description: 修改客户信息
     * */
    @RequestMapping("/updateMarketCustom/{customid}")
    public String updateMarketCustom(@PathVariable("customid") String customid, HttpSession session){
        CustomCondition condition=new CustomCondition();
        condition.setCustomid(Integer.parseInt(customid));
        Custom custom = marketService.queryOneCustom(condition);
        session.setAttribute("oneCustom",custom);
        return "market/customer/customerUpdate";
    }

    @RequestMapping("/updateCustom")
    @ResponseBody
    public String updateCustom(CustomCondition condition){
        int i = marketService.updateCustom(condition);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.market.controller
     * @NAME: updateCustom
     * @USER: WanWenCheng
     * @DATE: 2022/10/3
     * @TIME: 23：35
     * @Description: 分配功能
     * */
    @RequestMapping("/fenPei/{customid}")
    public String fenPei(@PathVariable("customid") String customid, HttpSession session){
        CustomCondition condition=new CustomCondition();
        condition.setCustomid(Integer.parseInt(customid));
        Custom custom = marketService.queryOneCustom(condition);
        session.setAttribute("fenpeiC",custom);
        return "forward:/market/queryAllJob";
    }

    @RequestMapping("/queryAllJob")
    @ResponseBody
    public String queryAllJob(){
        List<Job> job = marketService.queryAllJob();
        if (job!=null){
            if (!job.isEmpty()){
                return JSON.toJSONString(job);
            }
        }
        return JSON.toJSONString("false");
    }

    @RequestMapping("/queryUserByJob")
    @ResponseBody
    public String queryUserByJob(Job job){
        List<Users> users = marketService.queryUserByJob(job);
        if (users!=null){
            if (!users.isEmpty()){
                return JSON.toJSONString(users);
            }
        }
        return JSON.toJSONString("false");
    }

    @RequestMapping("/saveFenPei")
    @ResponseBody
    public String saveFenPei(Users user,HttpSession session){
        Custom fenpeiC = (Custom) session.getAttribute("fenpeiC");
        CustomCondition custom=new CustomCondition();
        custom.setCustomid(Integer.parseInt(fenpeiC.getCustomid()));
        custom.setServiceid(Integer.parseInt(user.getUId()));
        int i = marketService.saveFenPei(custom);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.market.controller
     * @NAME: updateCustom
     * @USER: WanWenCheng
     * @DATE: 2022/10/3
     * @TIME: 23：35
     * @Description: 增加客户信息
     * */
    @RequestMapping("/AddCustom")
    @ResponseBody
    public String AddCustom(CustomCondition condition){
        int i = marketService.AddCustom(condition);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }


    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.market.controller
     * @NAME: updateCustom
     * @USER: WanWenCheng
     * @DATE: 2022/10/3
     * @TIME: 23：35
     * @Description: 查询指定客户信息
     * */
    @RequestMapping("/queryAllCustomByCondition")
    @ResponseBody
    public String queryAllCustomByCondition(CustomCondition condition){
        List<Custom> customList = marketService.queryAllCustomByCondition(condition);
        if (customList!=null){
            if (!customList.isEmpty()){
                return JSON.toJSONString(customList);
            }
        }
        return JSON.toJSONString("null");
    }

}
